﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Timers;

namespace ARelogRelog
{
    public class InactivityWatcher : IDisposable
    {
        public event WarningEventHandler Warning;
        Timer m_pTimer;

        public InactivityWatcher(ARelog pARelog, double checkInterval, int warnOnHowManyInactiveMinutes)
        {
            ARelog = pARelog;
            CheckInterval = checkInterval;
            WarnOnHowManyInactiveMinutes = warnOnHowManyInactiveMinutes;

            m_pTimer = new Timer(checkInterval);
        }

        public void StartWatcher()
        {
            m_pTimer.Start();
            m_pTimer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
        }

        public void StopWatcher()
        {
            m_pTimer.Stop();
        }

        void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            m_pTimer.Enabled = false;

            foreach (ARelogMonitor pMonitor in ARelog.Monitors)
            {
                if (pMonitor.Enabled)
                {
                    DateTime lastActivity = GetLastActivity(pMonitor);
                    if (DateTime.Now.Subtract(lastActivity).TotalMinutes >= WarnOnHowManyInactiveMinutes)
                        OnWarning(pMonitor, lastActivity);
                }
            }

            m_pTimer.Enabled = true;
        }

        void OnWarning(ARelogMonitor pMonitor, DateTime lastActivity)
        {
            if (Warning != null)
                Warning(this, new WarningEventArgs(pMonitor, lastActivity));
        }

        public DateTime GetLastActivity(ARelogMonitor pMonitor)
        {
            string logDirPath = Path.Combine(Path.GetDirectoryName(pMonitor.BuddyPath), "Logs");

            DateTime lastWriteTime = DateTime.MinValue;
            foreach (string fn in Directory.GetFiles(logDirPath))
            {
                if (File.GetLastWriteTime(fn) > lastWriteTime)
                    lastWriteTime = File.GetLastWriteTime(fn);
            }

            return lastWriteTime;
        }

        public ARelog ARelog
        {
            get;
            private set;
        }

        public double CheckInterval
        {
            get;
            private set;
        }

        public int WarnOnHowManyInactiveMinutes
        {
            get;
            private set;
        }

        public class WarningEventArgs
        {
            public WarningEventArgs(ARelogMonitor pMonitor, DateTime lastActivity)
            {
                Monitor = pMonitor;
                LastActivity = lastActivity;
            }

            public ARelogMonitor Monitor
            {
                get;
                private set;
            }

            public DateTime LastActivity
            {
                get;
                private set;
            }
        }

        public delegate void WarningEventHandler (object sender, WarningEventArgs e);

        public void Dispose()
        {
            if (m_pTimer != null && m_pTimer.Enabled)
                m_pTimer.Stop();
        }
    }
}
